//
//  LoginViewController.m
//  ZhouProject
//
//  Created by Dlrj on 14-12-15.
//  Copyright (c) 2014年 admin. All rights reserved.
//

#import "LoginViewController.h"
#import "ZhuCeViewController.h"
#import "LoadingHelper.h"
#import "NetWorkingHelper.h"
#import "YongHuXinXi.h"
#import "MyApplyViewController.h"
#import "MyInfoViewController.h"
#import "XiaLa.h"
@interface LoginViewController ()

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"启明客户应用系统";
    UIButton *btnLeft = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    //    btnLeft.backgroundColor = [UIColor redColor];
    btnLeft.frame = CGRectMake(0, 0, 0, 0);
    [btnLeft setTitle:@"" forState:UIControlStateNormal];
    btnLeft.titleLabel.font = [UIFont systemFontOfSize:16.0f];
    UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithCustomView:btnLeft];
    self.navigationItem.leftBarButtonItem = back;
    
    UILabel * labPhone = [[UILabel alloc]initWithFrame:CGRectMake(kW(30), kH(150), kW(60), kH(25))];
    labPhone.text = @"手机号:";
    labPhone.textColor = [UIColor blackColor];
    [self.view addSubview:labPhone];
    
    UILabel * labPassword = [[UILabel alloc]initWithFrame:CGRectMake(kW(30), kH(220), kW(60), kH(25))];
    labPassword.text = @"密码:";
    labPassword.textColor = [UIColor blackColor];
    [self.view addSubview:labPassword];
    
    _txtPhone = [[UITextField alloc]initWithFrame:CGRectMake(kW(90), kH(145), kW(200), kH(35.5))];
    _txtPhone.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    _txtPhone.returnKeyType = UIReturnKeyDone;
    _txtPhone.delegate = self;
    _txtPhone.placeholder = @"手机号";
    UILabel *leftView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kW(5), kH(35.5))];
    leftView.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    [_txtPhone setLeftView:leftView];
    [_txtPhone setLeftViewMode:UITextFieldViewModeAlways];
    //边框宽度
    _txtPhone.layer.borderWidth = 1.0f;
    //边框颜色
    _txtPhone.layer.borderColor = [UIColor clearColor].CGColor;
    //内容不超出边框
    _txtPhone.clipsToBounds = YES;
    _txtPhone.clearButtonMode = UITextFieldViewModeAlways;
    _txtPhone.layer.cornerRadius = 5.0;
    [self.view addSubview:_txtPhone];
    
    _txtPassword = [[UITextField alloc]initWithFrame:CGRectMake(kW(90), kH(215), kW(200), kH(35.5))];
    _txtPassword.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    _txtPassword.returnKeyType = UIReturnKeyDone;
    _txtPassword.delegate = self;
    _txtPassword.placeholder = @"密码";
    leftView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kW(5), kH(35.5))];
    leftView.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    [_txtPassword setLeftView:leftView];
    [_txtPassword setLeftViewMode:UITextFieldViewModeAlways];
    _txtPassword.secureTextEntry = YES;
    _txtPassword.clearButtonMode = UITextFieldViewModeAlways;
    //边框宽度
    _txtPassword.layer.borderWidth = 1.0f;
    //边框颜色
    _txtPassword.layer.borderColor = [UIColor clearColor].CGColor;
    //内容不超出边框
    _txtPassword.clipsToBounds = YES;
    _txtPassword.layer.cornerRadius = 5.0;
    [self.view addSubview:_txtPassword];
    
    UIButton * btnLogin = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnLogin.frame = CGRectMake(kW(40), kH(300), kW(100), kH(35));
    [btnLogin setTitle:@"登录" forState:UIControlStateNormal];
    [btnLogin setBackgroundImage:[UIImage imageNamed:@"hb4"] forState:UIControlStateNormal];
    [btnLogin setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    btnLogin.backgroundColor = [UIColor whiteColor];
//    [btnLogin setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    btnLogin.layer.borderWidth = 1.0f;//边框宽度
//    btnLogin.layer.borderColor = [UIColor colorWithRed:212.0 / 255 green:212.0 / 255 blue:212.0 / 255 alpha:1].CGColor;//边框颜色
    btnLogin.layer.cornerRadius = 5.0;//设置圆角
    btnLogin.clipsToBounds = YES;//内容不超出边框
    [btnLogin addTarget:self  action:@selector(clickdenglu) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnLogin];
    
    UIButton * btnReg = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnReg.frame = CGRectMake(kW(180), kH(300), kW(100), kH(35));
    [btnReg setTitle:@"注册" forState:UIControlStateNormal];
    [btnReg setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnReg setBackgroundImage:[UIImage imageNamed:@"hb1"] forState:UIControlStateNormal];
//    btnReg.backgroundColor = [UIColor whiteColor];
//    [btnReg setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    btnReg.layer.borderWidth = 1.0f;//边框宽度
//    btnReg.layer.borderColor = [UIColor colorWithRed:212.0 / 255 green:212.0 / 255 blue:212.0 / 255 alpha:1].CGColor;//边框颜色
    btnReg.layer.cornerRadius = 5.0;//设置圆角
    btnReg.clipsToBounds = YES;//内容不超出边框
    [btnReg addTarget:self  action:@selector(clickzhuce) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnReg];

    
}
-(void)clickdenglu
{
    if (_txtPhone.text.length == 0) {
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请输入手机号" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
    }
    else if(_txtPassword.text.length == 0)
    {
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请输入密码" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
    }
    else
    {
        [LoadingHelper showLoadingWithView:self.view];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            
            YongHuXinXi * model = [NetWorkingHelper DengLuWithuserName:_txtPhone.text userPwd:_txtPassword.text];
            NSMutableArray * arrxiala = [NetWorkingHelper XiaLa];
            dispatch_async(dispatch_get_main_queue(), ^{
                [LoadingHelper hiddonLoadingWithView:self.view];
                if ([model.message isEqualToString:@"0"]) {
                    [self defaultSaveuseraccount:_txtPhone.text];
                    [self defaultSavePassword:_txtPassword.text];
                    self.navigationController.navigationBarHidden = YES;
                    NSMutableArray *arr = [[NSMutableArray alloc]init];
                    
                    MyApplyViewController *vc = [[MyApplyViewController alloc]init];
                    vc.strcategoryId = model.strcategoryId;
                    UINavigationController * nav = [[UINavigationController  alloc]initWithRootViewController:vc];
                    [nav.navigationBar setBarTintColor:RGBACOLOR(19, 93, 225, 1)];
                    UIColor * color = [UIColor whiteColor];
                    NSDictionary * dict=[NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
                    nav.navigationBar.titleTextAttributes = dict;
                    vc.tabBarController.hidesBottomBarWhenPushed = true;
                    vc.title = @"我的应用";
                    
                    
                    [arr addObject:nav];

                    MyInfoViewController *  vc1 = [[MyInfoViewController alloc]init];
                    vc1.struserId = model.struserId;
                    vc1.strloginName = model.strloginName;
                    vc1.strpassword = model.strpassword;
                    vc1.strcategoryId = model.strcategoryId;
                    vc1.strshopArea = model.strshopArea;
                    vc1.strshopName = model.strshopName;
                    vc1.arr = arrxiala;
                    UINavigationController * nav1 = [[UINavigationController  alloc]initWithRootViewController:vc1
                                                     ];
                    [nav1.navigationBar setBarTintColor:RGBACOLOR(19, 93, 225, 1)];
                    UIColor * color1 = [UIColor whiteColor];
                    NSDictionary * dict1=[NSDictionary dictionaryWithObject:color1 forKey:UITextAttributeTextColor];
                    nav1.navigationBar.titleTextAttributes = dict1;
                    vc1.tabBarController.hidesBottomBarWhenPushed = true;
                    vc1.title = @"我的资料";
                    
                    
                    
                    [arr addObject:nav1];
                   
                    
                    UITabBarController *bar = [[UITabBarController alloc] init];
                    bar.viewControllers = arr;
                    bar.selectedIndex = 0;
                    bar.delegate = self;
                    UITabBarItem * item = [bar.tabBar.items objectAtIndex:0];
                    [item setTitlePositionAdjustment:UIOffsetMake(0, -13)];
                    [item setTitleTextAttributes:  @{ NSFontAttributeName: [UIFont systemFontOfSize:20]} forState:UIControlStateNormal];
                    UITabBarItem * item1 = [bar.tabBar.items objectAtIndex:1];
                    [item1 setTitlePositionAdjustment:UIOffsetMake(0, -13)];
                    [item1 setTitleTextAttributes:  @{ NSFontAttributeName: [UIFont systemFontOfSize:20]} forState:UIControlStateNormal];[self.navigationController pushViewController:bar animated:YES];

                    
                   

                }
                else
                {
                    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"" message:@"登录失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
                    [alert show];
                }
        
            });
            
        });
    }
}
-(NSString *)defaultLoaduseraccount
{
    
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    return [defaults objectForKey:@"useraccount"];
    
}
-(void)defaultSaveuseraccount:(NSString*)a

{
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    
    [defaults setObject:a  forKey:@"useraccount"];
    
    [defaults synchronize];
    
}
-(NSString *)defaultLoadPassword
{
    
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    return [defaults objectForKey:@"Password"];
    
}
-(void)defaultSavePassword:(NSString*)a

{
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    
    [defaults setObject:a  forKey:@"Password"];
    
    [defaults synchronize];
    
}
-(void)clickzhuce
{
    ZhuCeViewController * vc = [[ZhuCeViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_txtPhone resignFirstResponder];
    [_txtPassword resignFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [_txtPhone resignFirstResponder];
    [_txtPassword resignFirstResponder];
    return YES;
}
-(BOOL)textFieldShouldClear:(UITextField *)textField
{
    return YES;
}
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField == _txtPhone) {              //手机号：length_11
        if (range.location >= 11) {
            return NO;
        }
        return YES;
    }
    else if (textField == _txtPassword){           //密码：length_15
        if (range.location >= 15) {
            return NO;
        }
        return YES;
    }
    else {
        return YES;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
